﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using BussinessObjects;
using BusinessObjects;
using LibraryO2S;
using System.IO;

public partial class controls_admin_NewsAdd : Pages_Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            try
            {
                loadSetting();
                base.loadModuleByType(lsbModule, 1);
                base.loadModuleByType(lsbModule, 14);
                base.loadModuleByType(lsbModule, 22);
                base.loadCategories(ddlCategories, 1);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally { base.CloseConnection(); }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            string filename = String.Empty;
            try
            {
                if (fulTHUMB_IMG.HasFile)
                {
                    filename = fulTHUMB_IMG.FileName;

                    int count = 0;
                    while (File.Exists(Server.MapPath("~/upload/news/") + filename))
                    {
                        filename = count + "_" + fulTHUMB_IMG.FileName;
                        count++;
                    }
                    fulTHUMB_IMG.SaveAs(Server.MapPath("~/upload/news/") + filename);
                }
            }
            catch (Exception ex)
            {
                divMessage.InnerHtml = O2S_Message.Error("Hệ Thống", "Lỗi ghi file! " + ex.Message);
                return;
            }
            int module_id = Convert.ToInt32(ddlCategories.SelectedValue);
            int status = 1;
            string title = txtFULL_ARTICLE_TITLE.Text.Trim();
            string full_article_description = txtFULL_ARTICLE_DESCRIPTION.Text.Trim();
            string details = fkcContent.Value;
            bool displayOnHome = cboDisplay.Checked;
            DataTable admin = (DataTable)Session["Admin"];
            string sort_display = txtSortDisplay.Text.Trim();
            string c_id = ";-1:" + sort_display + ";";
            string m_key = txtMetaKeyword.Text.Trim();
            string m_desc = "";// txtDescription.Text.Trim();
            foreach (ListItem item in lsbModule.Items)
            {
                if (item.Selected)
                    c_id += item.Value + ":" + sort_display + ";";
            }
            string strResult = BO_Articles.Insert(module_id, "",
                    2, "", filename, "", "", title, "", "", "", full_article_description, "", details, "", "", m_key, m_desc, 0,
                    0, 0, 0, displayOnHome, c_id,
                    DateTime.Now, admin.Rows[0]["USER_NAME"].ToString(), DateTime.Now, "", status);
            if (strResult != "0")
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Cập nhật thành công!");
            else
                divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Có lỗi trong quá trình xử lý!");
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally { base.CloseConnection(); }
    }
    private void loadSetting()
    {
        DataTable objDT = BO_Settings.SelectByGroupId(3, 1);
        foreach (DataRow dr in objDT.Rows)
        {
            string value = dr["SETTING_VALUE"].ToString();
            switch (dr["SETTING_KEY"].ToString())
            {
                case "NOTE_CATE":
                    NOTE_CATE.InnerText = value;
                    break;
                case "NOTE_DISPLAY":
                    NOTE_DISPLAY.InnerText = value;
                    break;
                case "NOTE_MODULE":
                    NOTE_MODULE.InnerText = value;
                    break;
                case "NOTE_SORT":
                    NOTE_SORT.InnerText = value;
                    break;
                case "NOTE_TITLE":
                    NOTE_TITLE.InnerText = value;
                    break;
                case "NOTE_DESCRIPT":
                    NOTE_DESCRIPT.InnerText = value;
                    break;
                case "NOTE_URL":
                    NOTE_URL.InnerText = value;
                    break;
            }
        }
    }
}
